#!/bin/sh

if [ $# -lt 4 ]; then
    echo "usage: ldtk-to-level INFILE OUTFILE DIMENSIONS SPRITEPERROW"
    exit 1
fi

INFILE="$1"
OUTFILE="$2"
DIMENSIONS="$3"
SPRITEPERROW="$4"

echo -n "offset\n0 0\n" >> $OUTFILE
cat $INFILE | jq -j '.levels | .[] | .layerInstances | reverse | .[] | "\nlayer\n\n", (.gridTiles | .[] | ("entity\n",.px[0], " ", .px[1], " "), (((.src[1] / '$DIMENSIONS') * '$SPRITEPERROW') + (.src[0] / '$DIMENSIONS')), "\n")' >> $OUTFILE
